package components.utils.file
{
	import capricornus.res.Image;
	
	import components.alert.Alert;
	import components.map.MapController;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	import flash.utils.ByteArray;

	public class FileManager
	{
		private static var _ins:FileManager;
		
		private var _selectFun:Function;		
		
		public function FileManager()
		{
		}
		
		public static function get instance():FileManager
		{
			if( _ins==null )
			{
				_ins = new FileManager();
			}
			return _ins;
		}
		
		private var _file:File;
		private var _byte:ByteArray;
		private var _backImageURL:String;
		private var _name:String;
		public function saveAs( name:String, byte:ByteArray, backImageURL:String ):void
		{
			_byte = byte;
			_name = name;
			_backImageURL = backImageURL;
			_file = new File();
			_file.addEventListener( Event.CANCEL, onCancelFile, false, 0, true );
			_file.addEventListener( Event.SELECT, onSelected, false, 0, true );
			_selectFun = onSelected;
			_file.browseForSave("");
		}
		
		private function clearFile():void
		{
			if( _file != null )
			{
				_file.removeEventListener( Event.CANCEL, onCancelFile );
				_file.removeEventListener( Event.SELECT, _selectFun );
				_file = null;
			}
			_byte = null;
		}
		
		private function onCancelFile( e:Event ):void
		{
			clearFile();
		}
		
		private function onSelected( e:Event ):void
		{
			var fs:FileStream
			if( _backImageURL != "" && _backImageURL != null )
			{
				var f:File = _file.parent;
				f = f.resolvePath( "MapConfig.xml" );
				var xml:XML = <root/>;
				xml.appendChild( new XML("<name>" + _name + "</name>") );
				xml.appendChild( new XML("<bg>" + _backImageURL + "</bg>") );
				
				fs = new FileStream();
				fs.open( f, FileMode.WRITE );
				fs.writeUTFBytes( xml.toXMLString() );
				fs.close();
			}
			fs = new FileStream();
			fs.open( _file, FileMode.WRITE );
			fs.writeUTFBytes( "MapData" );
			fs.writeBytes( _byte );
			fs.close();
			fs = null;
			clearFile();
		}
		
		public function open():void
		{
			_file = new File();
			_file.addEventListener( Event.CANCEL, onCancelFile, false, 0, true );
			_file.addEventListener( Event.SELECT, onSelectFileOpen, false, 0, true );
			_selectFun = onSelectFileOpen;
			_file.browseForOpen( "请选择地图数据文件：" );
		}
		
		private function onSelectFileOpen( e:Event ):void
		{
			var byte:ByteArray = new ByteArray();
			var fs:FileStream = new FileStream();
			fs.open( _file, FileMode.READ );
			if( fs.readUTFBytes( 7 )=="MapData" )
			{
				fs.readBytes( byte );
				var xml:XML = getConfigXML();
				
				MapController.instance.setMapData( byte, xml.name.toString(), xml.bg.toString() );
			}
			else
			{
				Alert.show( "该文件不是地图数据文件" );
			}
			fs.close();
			clearFile();
		}
		
		private function getConfigXML():XML
		{
			var f:File = _file.parent;
			f = f.resolvePath( "MapConfig.xml" );
			if( f.exists )
			{
				var fs:FileStream = new FileStream();
				fs.open( f, FileMode.READ );
				var s:String = fs.readUTFBytes( f.size );
				return new XML(s);
			}
			return new XML();
		}
		
		public function selectBackgoundImage():void
		{
			_file = new File();
			_file.addEventListener( Event.CANCEL, onCancelFile, false, 0, true );
			_file.addEventListener( Event.SELECT, onSelectImage, false, 0, true );
			_selectFun = onSelectImage;
			_file.browseForOpen( "请选择背景图：", [new FileFilter("Images", "*.jpg;*.png")] );
		}
		
		private function onSelectImage( e:Event ):void
		{
			var str:String = _file.nativePath;
			MapController.instance.setBGImage( str );
			clearFile();
		}
	}
}











